<template>
  <div>
    <el-card style="margin:5px;height: 70px;">
      <el-form inline="inline">
        <el-form-item label="字典名称">
          <el-input v-model="searchDict.name" @keydown.enter.prevent="loadDict" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="字典编码">
          <el-input v-model="searchDict.code" @keydown.enter.prevent="loadDict" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
          <el-button type="primary" @click="loadDict">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin:5px;">
      <div style="margin: 10px 0px 40px 10px;">
        字典列表
        <el-button @click="addDict" type="primary" style="float:right;margin-top: -3px;">新增字典</el-button>
      </div>
      <div>
        <el-table :data="dictArr">
          <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
          <el-table-column prop="name" label="字典编号" align="center"></el-table-column>
          <el-table-column prop="code" label="字典编码" align="center">
            <template #default="scope">
              <el-button @click="showDictOption(scope.row)" link type="primary">{{scope.row.code}}</el-button>
            </template>
          </el-table-column>
          <el-table-column  prop="remark" label="备注信息" align="center"></el-table-column>
          <el-table-column  prop="createTime" label="创建时间" align="center"></el-table-column>
          <el-table-column  prop="updateTime" label="更新时间" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button @click="editeDict(scope.row)" link type="primary" size="small">编辑</el-button>
              <el-button @click="delDict(scope.row.id)" link type="primary" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog :title="dialogTitle" v-model="dialogVisible"
               :close-on-click-modal="false" style="width:800px;padding:40px;">
      <el-form label-position="top" label-width="80px">
        <el-form-item label="字典名称">
          <el-input v-model="dict.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="字典编码">
          <el-input v-model="dict.code" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="dict.remark" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button @click="saveDict" type="primary">保存</el-button>
      </template>
    </el-dialog>
  </div>


</template>

<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import axios from "axios";
import {ElMessage} from "element-plus";

const searchDict=ref({})
const dictArr=ref([])
const reset = () => {
  searchDict.value={}
  loadDict()
}
const loadDict = () => {
  axios.post(BASE_URL+'/v1/dict/select',searchDict.value)
      .then((response)=>{
        if (response.data.code==2000){
          dictArr.value=response.data.data
          searchDict.value={}
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
onMounted(()=>loadDict())
const showDictOption = (row) => {
  router.push('/empHome/dictOption?id='+row.id+'&name='+row.name)
}
const dialogTitle=ref()
const dialogVisible=ref(false)
const dict=ref({})
const handleClose = () => {
  if (confirm("确认关闭弹窗吗")){
    dialogVisible.value=false
    dict.value={}
  }
}
const addDict = () => {
  dialogTitle.value="新增字典"
  dialogVisible.value=true
}
const editeDict = (row) => {
  loadDict()
  dict.value=row
  dialogTitle.value="编辑字典"
  dialogVisible.value=true
}
const saveDict = () => {
  if (confirm("确认保存吗")){
    axios.post(BASE_URL+'/v1/dict/save',dict.value)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("保存成功")
            dict.value={}
            dialogVisible.value=false
            loadDict()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const delDict = (id) => {
  if (confirm("确定要删除该字典吗，同时会删除该字典的所有字典项")){
    axios.delete(BASE_URL+'/v1/dict/delete/'+id)
        .then((response)=>{
          if (response.data.code==2000){
            let del={dictId:id}
            axios.delete(BASE_URL+'/v1/dictOption/delete',{data:del})
            .then((response)=>{
              if (response.data.code==2000){
                ElMessage.success("删除成功")
                loadDict()
              }else {
                ElMessage.error(response.data.msg)
              }
            })
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}

</script>

<style scoped>

</style>